import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';
import 'package:common/model/simple_user_info/simple_user_info.dart';

part 'user_bloc_state.g.dart';

@JsonSerializable(explicitToJson: true)
class UserBlocState {
  @JsonKey(name: 'userInfo')
  SimpleUserInfo userInfo;
  @JsonKey(name: 'type')
  int type;
  @JsonKey(name: 'token')
  String token;

  @JsonKey(name: 'imToken')
  String imToken;

  @JsonKey(name: 'imUserId')
  String imUserId;

  @JsonKey(name: 'status', toJson: userStatusToJson, fromJson: userStatusFromJson)
  UserStatus status;

  UserBlocState({this.userInfo, this.status, this.type, this.token, this.imToken, this.imUserId});

  Map<String, dynamic> toJson() => _$UserBlocStateToJson(this);

  String toJsonString() {
    return json.encode(toJson()).toString();
  }

  factory UserBlocState.fromJson(Map<String, dynamic> json) => _$UserBlocStateFromJson(json);

  factory UserBlocState.fromJsonString(String jsonString) => _$UserBlocStateFromJson(json.decode(jsonString));

  UserBlocState copy() {

    UserBlocState newState = UserBlocState();
    newState.status = status;
    newState.userInfo = userInfo;
    newState.type = type;
    newState.token = token;
    newState.imToken = imToken;
    newState.imUserId = imUserId;
    return newState;
  }
}

int userStatusToJson(UserStatus status) {
  return status.index;
}

UserStatus userStatusFromJson(int json) {
  return UserStatus.values[json];
}

enum UserStatus { Initializing, Idle, LoggingIn, LoggedIn, LoggedOut }
